var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTabsIndex: 0,
    txpop:false,
    totalnum:0,
    tx_page:1,
    dd_page:1,
    txlist:[],
    ddlist:[],
    total:'',
    tixiantrue:0,
    xxshow:false,
    totalpage:'',
    shezhishow:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var shop_type = wx.getStorageSync('shop_type');
    var role = wx.getStorageSync('role');
    if (options && parseInt(options.type11) == 1){
      this.setData({
        xxshow:true,
        shezhishow:false
      })
    }
    if (parseInt(shop_type) == 2 && parseInt(role) == 2){
      this.setData({
        xxshow: true
      })
    }

    
    this.gettotal();
    this.getorderlist();
    this.gettixian();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },
 
  // 获取资产
  gettotal:function(){
    var shop_id = wx.getStorageSync("shop_id");
    var that = this;
    var param = {
      url: "api/shop/myAssets",
      data: {
        shop_id: shop_id,
      },
      success: function (e) {
        console.log(e)
        that.setData({
          total: e.data.data,
        })
      },
    };
    app.post(param); 
  },
  // 获取订单明细
  getorderlist:function(){
    var shop_id = wx.getStorageSync("shop_id");
    var that = this;
    var param = {
      url: "api/shop/incomeDetail",
      data: {
        shop_id: shop_id,
        now_page: this.data.dd_page
      },
      success: function (e) {
        console.log(e)
        that.setData({
          ddlist: e.data.data.list,
          totalpage:e.data.data.total_page
        })
        // 隐藏加载框
        wx.hideLoading();
      },
    };
    app.post(param);   
  },
  // 获取提现记录
  gettixian:function(){
    var shop_id = wx.getStorageSync("shop_id");
    var that = this;
    var param = {
      url: "api/shop/withdrawList",
      data: {
        shop_id: shop_id,
        now_page: this.data.tx_page
      },
      success: function (e) {
        that.setData({
          txlist:e.data.data.list,
          totalpage: e.data.data.total_page
        })
        // 隐藏加载框
        wx.hideLoading();

      },
    };
    app.post(param);  
  },
  // tab切换
  navTap: function (e) {
    if (this.data.currentTabsIndex == e.currentTarget.dataset.index) {
      return false;
    } else {
      if (e.currentTarget.dataset.index == 0 ){
        this.setData({
          currentTabsIndex: e.currentTarget.dataset.index,
          dd_page:1,
          totalpage:0

        })
      } else if (e.currentTarget.dataset.index == 1 ){
        this.setData({
          currentTabsIndex: e.currentTarget.dataset.index,
          tx_page: 1,
          totalpage:0
        }) 
      }
     
    }
  },

  seehome:function(){
    wx.navigateTo({
      url: '/pages/index/index',

    })
  },
  showtx: function () {
    app.post({
        url:'api/shopr/judegShopBank',
        data:{
          shop_id:wx.getStorageSync('shop_id')
        },
        success:res=>{
            console.log(res)
            let data = res.data.data;
            if(data.need == 1){
                console.log(JSON.stringify(data))
                wx.showModal({
                  title:'提示',
                  content:'是否完善银行卡信息？',
                  cancelColor: '#999',
                  success:r=>{
                    if(r.confirm){
                        wx.navigateTo({
                          url: '/pages/mine/setBankCard/index?msg='+JSON.stringify(data),
                        })
                    }
                  }
                })
            }else{
              this.setData({
                  txpop: true
                })
            }
        }
    })
    
  },
  hidetx:function(){
    this.setData({
      txpop:false
    })
  },

  naviback:function(){
    wx.navigateBack({
      delta: 1
    })
  },
  inblur:function(e){
    var txnum = e.detail.value;
    var totalnum = this.data.total.money;
    console.log(txnum)
    console.log(totalnum)
    if (txnum == ''|| Number(txnum) == 0 || Number(txnum) > Number(totalnum)) {
      wx.showToast({
        title: '核对提现金额',
        icon: "none",
        duration: 2000
      })
    }else{
      this.setData({
        tixiantrue:1
      })
    }
  },
  tixian:function(e){
      var txnum = e.detail.value.txnum;
      var totalnum = this.data.total.money;
      if(txnum == '' || txnum > totalnum){
          wx.showToast({
            title: '核对提现金额',
            icon:"none",
            duration:2000
          })
          return false;
      }
      var userid = wx.getStorageSync('userId');
      var shopid = wx.getStorageSync('shop_id');
      var that = this;
      var param = {
        url:"api/shop/withdraw",
        data:{
          user_id:userid,
          shop_id:shopid,
          amount:txnum
        },
        success:function(e){
          wx.showToast({
            title: '提交成功，一到2个工作日到账',
            icon:"none",
            duration:2000
          })
          that.setData({
            txpop: false
          })
          setTimeout(function(){
            that.gettotal();
          },2000)
        }
      }
      app.post(param)
  },

  // 专属码
  ewm:function(){
      wx.navigateTo({
        url: '/pages/shangjia/erweima/erweima',
      })
  },
  onReachBottom: function () {
    var that = this;
    var shop_id = wx.getStorageSync("shop_id");
    var userid = wx.getStorageSync('userId');
    if (that.data.currentTabsIndex == 0){
      // 显示加载图标
      
      // 页数+1
      var dd_page = parseInt(that.data.dd_page)+1;
      if (dd_page > parseInt(that.data.totalpage)){
        wx.showToast({
          title: '没有更多数据了',
          icon: "none",
          duration: 2000
        })
        return false;
      }else{
        wx.showLoading({
          title: '玩命加载中',
        })
        var param = {
          url: "api/shop/incomeDetail",
          data: {
            shop_id: shop_id,
            now_page: dd_page
          },
          success: function (e) {
            console.log(e)
            that.setData({
              dd_page: dd_page,
              ddlist: that.data.ddlist.concat(e.data.data.list),
            })
            // 隐藏加载框
            wx.hideLoading();

          },
        };
        app.post(param);
      }
    }
    
    if (that.data.currentTabsIndex == 1){
      // 显示加载图标
      
      // 页数+1
      var tx_page = that.data.tx_page + 1;
      if (tx_page > that.data.totalpage) {
        wx.showToast({
          title: '没有更多数据了',
          icon: "none",
          duration: 2000
        })
        return false;
      } else {
        wx.showLoading({
          title: '玩命加载中',
        })
        var param = {
          url: "api/shop/withdrawList",
          data: {
            shop_id: shop_id,
            tx_page: tx_page
          },
          success: function (e) {
            that.setData({
              tx_page: tx_page,
              txlist: that.data.txlist.concat(e.data.data.list),
            })
            // 隐藏加载框
            wx.hideLoading();

          },
        };
        app.post(param);
      }
    }
  },
  seexx:function(){
    wx.navigateTo({
      url: '/pages/shangjia/xiaoxi/xiaoxi',
    })
  },
  seeshezhi:function(){
    var shop_type = wx.getStorageSync('shop_type');
    if (parseInt(shop_type) == 1) {
      wx.navigateTo({
        url: '/pages/shangjia/wash-shezhi/wash-shezhi',
      })
    }
    if (parseInt(shop_type) == 3) {
      wx.navigateTo({
        url: '/pages/shangjia/jiayou-shezhi/jiayou-shezhi',
      })
    }
  }
})